package LeetCode;

import java.util.LinkedList;
import java.util.Queue;

public class Code101_isSymmetric {

    public boolean isSymmetric(TreeNode root) {
        return isMirror(root, root);
    }
    // node1,node2分别表示两棵树的对称节点
    private boolean isMirror(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null){
            return true;
        }
        if (node1 == null || node2 == null){
            return false;
        }
        if (node1.val == node2.val){
            return isMirror(node1.left, node2.right) && isMirror(node1.right, node2.left);
        } else {
            return false;
        }
//        return node1.val == node2.val && isMirror(node1.left, node2.right) &&
//                isMirror(node1.right, node2.left);
    }

    // 迭代
    public boolean isSymmetric2(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode t1 = root;
        TreeNode t2 = root;
        queue.offer(t1);
        queue.offer(t2);
        while (!queue.isEmpty()){
            t1 = queue.poll();
            t2 = queue.poll();
            if (t1 == null && t2 == null){
                continue;
            }
            if (t1 == null || t2 == null){
                return false;
            }
            if (t1.val == t2.val){
                queue.offer(t1.left);
                queue.offer(t2.right);
                queue.offer(t1.right);
                queue.offer(t2.left);
            } else {
                return false;
            }
        }
        return true;
    }
}
